﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MWTools.Client.Common;
using MWTools.Client.Common.Interfaces;
using MWTools.Client.Dal;

namespace MWTools.Client
{
    public class AppContext
    {
        private static AppContext _instance = new AppContext();


        public UserSetting UserSetting { get; private set; }


        public void EnsureSettingsData()
        {
            Logger.DebugFormat("EnsureSettingsData", "");
            OutlookSetupHelper.EnsureSettingsData();
        }

        private AppContext()
        {
            try
            {   
                EnsureSettingsData();
                System.Data.DataTable dt = Dal.UserSettingDal.UserSettingSelect(null);

                if(dt == null || dt.Rows.Count == 0)
                {
                    UserSetting = GetDefaultSetting();
                }

                UserSetting = new Common.Interfaces.UserSetting();
                UserSetting.UserName = dt.Rows[0]["UserName"].ToString();
                UserSetting.MWToolsServerUri = dt.Rows[0]["MWToolsServerUri"].ToString();
                UserSetting.Password = dt.Rows[0]["Password"].ToString();
                UserSetting.DataLastRefreshedOn = Convert.ToDateTime(dt.Rows[0]["DataLastRefreshedOn"]);
                UserSetting.LogFilePath = dt.Rows[0]["LogFilePath"].ToString();
                UserSetting.CurrentPlugin = dt.Rows[0]["CurrentPlugin"].ToString();
                UserSetting.PluginAssembly = dt.Rows[0]["PluginAssembly"].ToString();
            }
            catch (Exception ex)
            {

                Logger.Error("AppContext", ex);

            }
        }

        public static UserSetting GetDefaultSetting()
        {
            UserSetting setting = new UserSetting();
            setting.UserName = "";
            setting.Password = "";
            
            setting.MWToolsServerUri = Constants.MWTools_SERVER_URI;
            setting.AppDataRootPath = Constants.APPDATA_ROOT_PATH;
            setting.LogFilePath = Constants.LOG_FILE_PATH;
            setting.DataLastRefreshedOn = DateTime.Now.AddDays(-1);
            setting.CurrentPlugin = "MWTools.Client.Common.Interfaces.ContentSubmitterBase";

            return setting;
        }

        public static AppContext Current
        {
            get
            {
                if (_instance == null)
                {
                    lock (_instance)
                    {
                        _instance = new AppContext();
                    }
                }

                return _instance;
            }
        }

        public void Save()
        {
            EnsureSettingsData();
            UserSettingDal.UserSettingDelete(null);
            UserSettingDal.UserSettingInsert(AppContext.Current.UserSetting);

        }

        public void Load(UserSetting setting)
        {
            UserSetting = setting;
        }
    }
}
